﻿using System;

namespace Backend.JYD.Common
{
    public class DateTimeHelper
    {
        #region 字段
        
        /// <summary>
        /// yyyyMMddHHmmss
        /// </summary>
        public const string LONG_FORMAT_DATE_STRING = "yyyyMMddHHmmss";
        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        public const string LONG_FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
        /// <summary>
        /// yyyyMMddHHmmss
        /// </summary>
        public const string LONG_FORMAT_EXCEPT_SECOND = "yyyy-MM-dd HH:mm";
        /// <summary>
        /// yyyy-MM-dd
        /// </summary>
        public const string SHORT_FORMAT = "yyyy-MM-dd";
        /// <summary>
        /// yyyy-MM
        /// </summary>
        public const string YEAR_AND_MONTH = "yyyy-MM";
        /// <summary>
        /// yyyyMM
        /// </summary>
        public const string YEAR_MONTH = "yyyyMM";
        /// <summary>
        /// yyyyMMdd
        /// </summary>
        public const string YEAR_MONTH_DAY = "yyyyMMdd";
        /// <summary>
        /// yyyy-MM-dd HH:mm:ss.ffffff
        /// </summary>
        public const string MILLI_SECOND_DATE_TIME = "yyyy-MM-dd HH:mm:ss.ffffff";
        /// <summary>
        /// yyyyMMddHHmmssffffff
        /// </summary>
        public const string MILLI_SECOND_DATE_STRING = "yyyyMMddHHmmssffffff";

        #endregion

        #region 构造函数
        
        public DateTimeHelper() { }

        #endregion

        #region 公共函数

        private static DateTime DATETIME_MINVALUE
        {
            get { return new DateTime(1900, 1, 1);}
        }

        public static bool IsMinValue(DateTime dt)
        {
            return DateTime.MinValue == dt || DateTimeHelper.DATETIME_MINVALUE == dt;
        }

        /// <summary>
        /// 将时间转化成：yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDateLongFormat(DateTime dt)
        {
            if (dt == null)
            {
                return null;
            }
            return dt.ToString(DateTimeHelper.LONG_FORMAT_DATE_TIME);
        }

        /// <summary>
        /// 将时间转化成：yyyy-MM-dd
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDateShortFormat(DateTime dt)
        {
            if (dt == null)
            {
                return null;
            }
            if (DateTimeHelper.IsMinValue(dt))
            {
                return string.Empty;
            }
            return dt.ToString(DateTimeHelper.SHORT_FORMAT);
        }

        public static string GetDateFormart(DateTime dt, string formart)
        {
            if (dt == null)
            {
                return null;
            }
            if (DateTimeHelper.IsMinValue(dt))
            {
                return string.Empty;
            }
            return dt.ToString(formart);
        }
            
        /// <summary>
        /// 将时间字符串转化为DateTime，转化失败，则返回当前时间。
        /// </summary>
        /// <returns></returns>
        public static DateTime Str2DateTime(string strDate)
        {
            return Str2DateTime(strDate, DateTime.Now);
        }

        /// <summary>
        /// 将时间字符串转化为DateTime，转化失败，则返回默认时间时间。
        /// </summary>
        /// <param name="strDate">转化参数</param>
        /// <param name="defDate">失败后的默认时间</param>
        /// <returns></returns>
        public static DateTime Str2DateTime(string strDate, DateTime defDate)
        {
            if(string.IsNullOrEmpty(strDate)) return defDate;

            try
            {
                DateTime dt = Convert.ToDateTime(strDate);
                return dt;
            }
            catch
            {
                return defDate;
            }
        } 

        #endregion
    }
}
